var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
    hiddenmodalput: true,
		paypwd:'',
		couponList: [],
		couponrid:0,
		coupontype:1,
		usescore: 0,
		money: 0,
		disprice: 0,
		dkmoney:0,
		couponmoney:0,
		paymoney: 0,
		mdkey:0,
		couponvisible:false,
		bid:0,
	},
	_onLoad: function () {
		var that = this;
		var bid = 0
		if (that.options && that.options.bid) {
			bid = that.options.bid
		}
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				bid = parseInt(scenes[1]);
			}
		}
		that.setData({bid:bid})
		//获取产品信息
		$.get('bmspay/pay', {bid:bid}, function (res) {
			if (res.status == 0) {
				$.alert(res.msg,function(){
					$.goback();
				})
				return;
			}
			that.setData(res)
		});
	},
	inputMoney: function (e) {
		console.log(e)
		var money = e.detail.value;
		if(!money) money = 0
		var money = parseFloat(money);
		if(money <=0) money = 0;
		this.setData({
			money: money
		})
	},
	inputScore: function (e) {
		console.log(e)
		var score = e.detail.value;
		if(!score) score = 0
		var score = parseFloat(score);
		if(score <=0) score = 0;
		this.setData({
			score: score
		})
	},
	getpwd:function(e){
		var paypwd = e.detail.value;
		this.setData({
			paypwd: paypwd
		});
	},
	cancel: function () {
		this.setData({
			hiddenmodalput: true
		});
	},
	modalinput: function () {
		this.setData({
			hiddenmodalput: !this.data.hiddenmodalput
		})
	},
	topay: function (e) {
		var that = this
		//var typeid = e.detail.value.paytype;
    var typeid = e.currentTarget.dataset.typeid;
		var money = that.data.money
		var score = that.data.score
		if (typeid == 2) {
			that.setData({hiddenmodalput:true})
			$.confirm('确定支付吗?', function () {
				$.post('bmspay/pay', {bid:that.data.bid,typeid: typeid,paypwd:that.data.paypwd,money:money,score:score}, function (data) {
					if (data.status == 0) {
						$.error(data.msg);
						return;
					}
					if (data.status == 2) { //无需付款
						$.success(data.msg);
						setTimeout(function () {
							$.goto('paylog');
						}, 1000);
						return;
					}
				}, '提交中');
			})
		} else {
			$.post('wxpay/pay', {bid:that.data.bid,typeid: typeid,money:money,couponrid:couponrid,usescore:usescore,mdid:mdid}, function (data) {
				if (data.status == 0) {
					$.error(data.msg);
					return;
				}
				if (data.status == 2) { //无需付款
					$.success(data.msg);
					setTimeout(function () {
						$.goto('paylog');
					}, 1000);
					return;
				}
				var opt = data.data;
				wx.requestPayment({
					'timeStamp': opt.timeStamp,
					'nonceStr': opt.nonceStr,
					'package': opt.package,
					'signType': 'MD5',
					'paySign': opt.paySign,
					'success': function (res) {
						$.success('付款完成');
						setTimeout(function () {
							$.goto('paylog');
						}, 1000)
					},
					'fail': function (res) { }
				});
			}, '提交中');
		}
	},
	showCouponList:function(){
		this.setData({couponvisible:true})
	},
	handleClickMask:function(){
		this.setData({couponvisible:false})
	},
	GetDistance:function(lat1,  lng1,  lat2,  lng2){
		var radLat1 = lat1*Math.PI / 180.0;
		var radLat2 = lat2*Math.PI / 180.0;
		var a = radLat1 - radLat2;
		var  b = lng1*Math.PI / 180.0 - lng2*Math.PI / 180.0;
		var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) +
		Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
		s = s *6378.137 ;// EARTH_RADIUS;
		s = Math.round(s * 100) / 100;
		return s;
	},
	compare:function(property){
		return function(a,b){
			var value1 = a[property];
			var value2 = b[property];
			return value1 - value2;
		}
	}
}));